﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace _013_WCFService
{
    [ServiceBehavior (InstanceContextMode = InstanceContextMode.PerSession)]
    class Server : IServerInterface
    {
        private Storage _storage = new Storage();

        public int AddRequest(int[] rq)
        {
            int index = _storage.Requests.Count;
            _storage.Requests.Add(index, rq);
            return index;
        }

        public int[] GetRequest(int rq_id)
        {
            if (_storage.Requests.ContainsKey(rq_id))
            {
                return _storage.Requests[rq_id];
            }
            else throw new ArgumentException("Request not found");

        }

        public void ProcessRequest(int rq_id)
        {
            Console.WriteLine("Processing ...");
            if (_storage.Requests.ContainsKey(rq_id))
            {
                int[] rq = _storage.Requests[rq_id];
                int[] resp = new int[rq.Length];

                for(int i=0;i<resp.Length;i++) resp[i] = rq[rq.Length-i-1];

                if (_storage.Response.ContainsKey(rq_id)) _storage.Response[rq_id] = resp;
                else                _storage.Response.Add(rq_id, resp);
            }
            else throw new ArgumentException("Request not found");
        }

        public int[] GetResponse(int rq_id)
        {
            if (_storage.Response.ContainsKey(rq_id))
            {
                return _storage.Response[rq_id];
            }
            else throw new ArgumentException("Response not found");
        }
    }
}
